package cn.etgps.etgps.dialog;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnKeyListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;

import cn.etgps.etgps.R;
import cn.etgps.etgps.utils.MainUsedInterface;

/**
 * Control dialog in whole project
 *
 * @author nicky.han
 */
@SuppressLint("ValidFragment")
public class DialogCreater extends DialogFragment {
    public static final int DIALOG_EXIT = 10001;
    public static final int DIALOG_PROGRESS = 10002;
    /**
     * Get activity res
     */
    private Context mContext = null;
    /**
     * Notify activity when clicked button
     */
    private MainUsedInterface.MainButtonClickListener mListener = null;

    public DialogCreater(Context context, MainUsedInterface.MainButtonClickListener listener) {
        super();
        mContext = context;
        mListener = listener;
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }

    @Override
    public Dialog getDialog() {
        return super.getDialog();
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        int title = getArguments().getInt(mContext.getResources().getString(R.string.dialog_fragment_key));
        switch (title) {
            case DIALOG_EXIT:
                return createExitAppDialog();
            case DIALOG_PROGRESS:
                return createProgressDialog();
            default:
                break;
        }
        return super.onCreateDialog(savedInstanceState);
    }

    @Override
    public void show(FragmentManager manager, String tag) {
        super.show(manager, tag);
    }

    /**
     * Show dialog by dialog id
     */
    public void showDialog(FragmentManager manager, int id) {
        try {
            Bundle args = new Bundle();
            args.putInt(mContext.getResources().getString(R.string.dialog_fragment_key), id);
            this.setArguments(args);
            this.show(manager, mContext.getResources().getString(R.string.dialog_fragment_tag));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Dialog createProgressDialog() {
        OnKeyListener onKeyListener = new OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface arg0, int arg1, KeyEvent arg2) {
                return true;
            }
        };
        LayoutInflater mInflater = LayoutInflater.from(mContext);
        View convertView = mInflater.inflate(R.layout.progress_dialog_view, null);
        Dialog dialog = new Dialog(mContext, R.style.CustomProgressDialog);
        dialog.setContentView(convertView);
        dialog.setCancelable(false);
        dialog.setCanceledOnTouchOutside(false);
        dialog.setOnKeyListener(onKeyListener);
        return dialog;
    }

    private AlertDialog createExitAppDialog() {

        DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int which) {
                mListener.clickButtonListener("Exit_app");
            }
        };
        return new AlertDialog.Builder(mContext).setTitle(R.string.exit_dialog_title).setMessage(
                R.string.exit_app_dialog_message).setPositiveButton(R.string.btn_ok, listener).setNegativeButton(
                R.string.btn_cancel, null).create();
    }
}
